package com.javaxiaobear.base.common.utils;

import com.javaxiaobear.base.common.utils.sql.SqlUtil;
import com.javaxiaobear.base.framework.web.page.PageDomain;
import com.javaxiaobear.base.framework.web.page.TableSupport;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

/**
 * 分页工具类
 *
 * @author javaxiaobear
 */
public class PageUtils {
  /** 设置请求分页数据 */
  public static <E> Page<E> getPage() {
    return getPage(null);
  }

  public static <E> Page<E> getPage(QueryWrapper wrapper) {
    PageDomain pageDomain = TableSupport.buildPageRequest();
    Integer pageNum = pageDomain.getPageNum();
    Integer pageSize = pageDomain.getPageSize();
    String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
    Boolean reasonable = pageDomain.getReasonable();
    if (wrapper != null) wrapper.orderBy(orderBy);
    return new Page<>(pageNum, pageSize);
  }
}
